/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.dialog.setup.controller;

import java.awt.CardLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JLabel;
import offset.nodes.client.model.ActivePanelContainer;
import offset.nodes.client.model.Initializable;
import offset.nodes.client.dialog.newnode.model.SetupModel;
import offset.nodes.client.dialog.newnode.model.ObjectType;
import offset.nodes.client.dialog.newnode.model.Step;
import offset.nodes.client.model.Validator;
import offset.nodes.client.dialog.setup.view.SetupDialog;

/**
 *
 * @author Walter Lütgenau
 */
public class SetupActions {
    
    public static abstract class AbstractNewNodeAction extends AbstractAction implements ActivePanelContainer {
        SetupModel model;
        SetupDialog dialog;
    
        public AbstractNewNodeAction(SetupModel model, SetupDialog dialog) {
            this.model = model;
            this.dialog = dialog;
        }

        @Override
        public void executeOk() {
            dialog.getNextButton().doClick();
        }

        @Override
        public void enableOkButton(boolean enable) {
            dialog.getNextButton().setEnabled(enable);
        }
        
        public void update() {   
            JLabel[] lables = { dialog.getJLabel1(), dialog.getJLabel2(), dialog.getJLabel3(), dialog.getJLabel4(),
                dialog.getJLabel5() };
            
            ObjectType currentType = model.getObjectTypes()[model.getCurrentObjectType()];
            Step currentStep = currentType.getSteps()[model.getCurrentStep()];            
            
            if ( model.getCurrentStep() > 0 )
                dialog.getBackButton().setEnabled(true);
            else
                dialog.getBackButton().setEnabled(false);
            
            if (currentType.getSteps().length > model.getCurrentStep() + 1) {
                dialog.getNextButton().setEnabled(true);
                dialog.getFinishButton().setEnabled(false);
            }
            else {
                dialog.getNextButton().setEnabled(false);
                dialog.getFinishButton().setEnabled(true);
            }
            
            for( int i=0; i<model.getCurrentStep(); i++) {
                lables[i].setFont(lables[i].getFont().deriveFont(Font.PLAIN));
                lables[i].setText(currentType.getSteps()[i].getName());
            }
            
            for( int i=model.getCurrentStep(); i<currentType.getSteps().length; i++) 
                lables[i].setText("");
            
            
            if ( model.getCurrentStep() >= 0) {
                JLabel currentLabel =  lables[model.getCurrentStep()];

                currentLabel.setFont(currentLabel.getFont().deriveFont(Font.BOLD));
                currentLabel.setText(currentType.getSteps()[model.getCurrentStep()].getName());
            }
            
            //dialog.getCurrentStepPanel().add(currentStep.getPanel(), currentStep.getName() );
            if ( currentStep.getPanel() instanceof Initializable)
                ((Initializable)currentStep.getPanel()).init(this);
            ((CardLayout)dialog.getCurrentStepPanel().getLayout()).show(dialog.getCurrentStepPanel(), currentStep.getKey());
            dialog.getCurrentStepLabel().setText(currentStep.getName());
        }
    }
    
    public static class PreviousAction extends AbstractNewNodeAction {
        public PreviousAction(SetupModel model, SetupDialog dialog) {
            super(model,dialog);
        }
        
        public void actionPerformed(ActionEvent e) {
           if ( model.getCurrentStep() > 0)
                model.setCurrentStep(model.getCurrentStep()-1);
            update();
        }
    }
    
    public static class NextAction extends AbstractNewNodeAction {
        public NextAction(SetupModel model, SetupDialog dialog) {
            super(model,dialog);
        }
        
        public void actionPerformed(ActionEvent e) {
            Step currentStep = model.getObjectTypes()[model.getCurrentObjectType()].getSteps()[model.getCurrentStep()];
            if ( currentStep.getPanel() instanceof Validator ) {
                Validator validator = (Validator) currentStep.getPanel();
                if ( !validator.isValidPane())
                    return;
            }
            if ( model.getCurrentStep() < model.getObjectTypes()[model.getCurrentObjectType()].getSteps().length - 1)
                model.setCurrentStep(model.getCurrentStep()+1);
            update();
        }
    }
    
    public static class FinishAction extends AbstractNewNodeAction {
        public FinishAction(SetupModel model, SetupDialog dialog) {
            super(model,dialog);
        }
        
        public void actionPerformed(ActionEvent e) {
            Step currentStep = model.getObjectTypes()[model.getCurrentObjectType()].getSteps()[model.getCurrentStep()];
            if ( currentStep.getPanel() instanceof Validator ) {
                Validator validator = (Validator) currentStep.getPanel();
                if ( !validator.isValidPane())
                    return;
            }
            dialog.doClose(SetupDialog.RET_OK);
        }
    }
    
    public static class CancelAction extends AbstractNewNodeAction {
        public CancelAction(SetupModel model, SetupDialog dialog) {
            super(model,dialog);
        }
        
        public void actionPerformed(ActionEvent e) {
            dialog.doClose(SetupDialog.RET_CANCEL);
        }
    }
}
